﻿using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

public static class GameObjectUtility {

    public static void ForceRefreshSizeFitter(this GameObject gameObject) {
        Assert.IsNotNull(gameObject);

        var sizeFitters = gameObject.GetComponentsInChildren<ContentSizeFitter>();
        for (var index = sizeFitters.Length - 1; index >= 0; --index) {
            var sizeFitter = sizeFitters[index];
            LayoutRebuilder.ForceRebuildLayoutImmediate(sizeFitter.transform as RectTransform);
        }
        var layoutGroups = gameObject.GetComponentsInChildren<LayoutGroup>();
        for (var index = layoutGroups.Length - 1; index >= 0; --index) {
            var layoutGroup = layoutGroups[index];
            LayoutRebuilder.ForceRebuildLayoutImmediate(layoutGroup.transform as RectTransform);
        }
    }
}